/**
 * Copyright (c) 2015-2016, Javen Zhou  (javen205@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.javen.weixin.menu;

import com.jfinal.kit.JsonKit;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.MenuApi;

/**
 * 菜单管理器类
 * 
 * @author Javen 2016年5月30日
 */
public class MenuManager {
	public static void main(String[] args) {
		// 将菜单对象转换成json字符串
		// 有问题：主菜单项多了一个type
		String jsonMenu = JsonKit.toJson(getTestMenu()).toString();
		System.out.println(jsonMenu);
		ApiConfig ac = new ApiConfig();

		// 配置微信 API 相关常量
		String appId = "wx56b4b611da117627";
		ac.setAppId(appId);
		ac.setAppSecret("65294b975b420b74913d4223e63ae58c");
		// ac.setAppId(PropKit.get("appId"));
		// ac.setAppSecret(PropKit.get("appSecret"));
		ApiConfigKit.putApiConfig(ac);
		ApiConfigKit.setThreadLocalAppId(appId);

		// 创建菜单
		ApiResult apiResult = MenuApi.createMenu(jsonMenu);
		System.out.println(apiResult.getJson());
	}

	/**
	 * 组装菜单数据
	 * 
	 * @return
	 */
	private static Menu getTestMenu() {
		ViewButton btn1 = new ViewButton();
		btn1.setName("无卡支付");
		btn1.setType("view");
		btn1.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx56b4b611da117627&"
				+ "redirect_uri=http://hushangkui.free.ngrok.cc/oauth&response_type=code&scope=snsapi_userinfo&state=pay#wechat_redirect");

		ViewButton btn2 = new ViewButton();
		btn2.setName("个人中心");
		btn2.setType("view");
		btn2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx56b4b611da117627"
				+ "&redirect_uri=http://hushangkui.free.ngrok.cc/oauth&response_type=code&scope=snsapi_userinfo&state=my#wechat_redirect");
		
		ViewButton btn31 = new ViewButton();
		btn31.setName("商户绑定");
		btn31.setType("view");
		btn31.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx56b4b611da117627"
				+ "&redirect_uri=http://hushangkui.free.ngrok.cc/oauth&response_type=code&scope=snsapi_userinfo&state=bind#wechat_redirect");
		
		ViewButton btn32 = new ViewButton();
		btn32.setName("交易查询");
		btn32.setType("view");
		btn32.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx56b4b611da117627"
				+ "&redirect_uri=http://hushangkui.free.ngrok.cc/oauth&response_type=code&scope=snsapi_userinfo&state=search#wechat_redirect");

		ComButton btn3 = new ComButton();
		btn3.setName("商户查询");  
		btn3.setSub_button(new Button[] { btn31, btn32});  
		
		Menu menu = new Menu();
		menu.setButton(new Button[] { btn1, btn2, btn3});
		return menu;
	}
}
